﻿using CTP.CRM.Core.Model;
using Serilog.Events;
using Serilog;

namespace CTP.CRM.Core.Logger
{
    public static class SerilogHostingExtensions
    {
        public static WebApplicationBuilder UseSerilogDefault(this WebApplicationBuilder builder, string consoleOutputTemplate = "")
        {
            ConfigurationManager configuration = builder.Configuration;
            if (string.IsNullOrEmpty(consoleOutputTemplate))
            {
                Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).CreateLogger();
            }
            else
            {
                Log.Logger = new LoggerConfiguration().WriteTo.Console(LogEventLevel.Verbose, consoleOutputTemplate).CreateLogger();
            }

            string text = configuration["Environment"];
            NoticeOptions noticeOptions = configuration.GetSection("Notice")?.Get<NoticeOptions>();
            if (noticeOptions == null)
            {
                Console.Write("-- 未找到日志飞书通知相关配置");
            }
            else
            {
                LogHelper.Init(noticeOptions, text == "uat" || text == "Product");
            }

            builder.Host.UseSerilog();
            return builder;
        }
    }
}
